package com.hfy.music.controller;

import com.hfy.model.dto.together.CreateRoomDto;
import com.hfy.model.dto.together.GenerateInviteDto;
import com.hfy.model.dto.together.InviteDto;
import com.hfy.music.service.ListenMusicTogetherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import result.Result;

/**
 * @ author 韩
 * time: 2024/10/7 12:16
 */

@RestController
@RequestMapping("/together")
@Tag(name = "一起听功能接口控制器" ,description = "使用接口配合websocket的逻辑共同完成一起听功能")
public class ListenMusicTogetherController {

    @Autowired
    private ListenMusicTogetherService listenMusicTogetherService;

    /**
     * 这个一起听房间邀请的逻辑是，房主可以生成一个邀请链接，然后点击加入一起听后，就会请求服务器实现邀请逻辑，加入到该房间内
     */
    @PostMapping("/accept_invite")
    @Operation(summary = "接收邀请，进行加入房间，建立websocket连接等操作")
    @Parameters({@Parameter(name = "roomId", description = "该条邀请id，也就是房间号"),
                @Parameter(name = "inviteeAccount", description = "受邀用户的账号")

    })
    public Result acceptInvite(@RequestBody InviteDto dto) {
        return listenMusicTogetherService.acceptInvite(dto);
    }



    /**
     *  本来想直接把邀请的信息直接放在url上，不走数据库了，这样一想弊端还挺多的，过期时间无法保证，用户也可以随意构造邀请信息
     *  虽然是个人开发，做就尽量做到严谨吧
     */

    @PostMapping("/generate_invite")
    @Operation(summary = "生成邀请信息")
    @Parameters({@Parameter(name = "inviterAccount", description = "邀请者账号"),
            @Parameter(name = "roomId", description = "房间号"),
            @Parameter(name = "musicId", description = "音乐id"),
    })
    public Result generateInviteInfo(@RequestBody GenerateInviteDto dto) {
        return listenMusicTogetherService.generateInviteInfo(dto);
    }

    /**
     *
     */

    @PostMapping("/create_room")
    @Operation(summary = "创建一起听房间", description = "在redis里创建房间号，把房主加进去，并把房间号返回")
    @Parameters({@Parameter(name = "inviterAccount", description = "邀请者账号"),
    })
    public Result createRoom(@RequestBody CreateRoomDto dto) {
        return listenMusicTogetherService.createRoom(dto);
    }

    @GetMapping("/invite_info")
    @Operation(summary = "渲染邀请页面", description = "根据房间号对数据库进行查询，查询到对应的信息")
    @Parameters({@Parameter(name = "roomId", description = "房间号"),
    })
    public Result getInviteInfo(@RequestParam("roomId") String roomId) {
        return listenMusicTogetherService.getInviteInfo(roomId);
    }



    @GetMapping("/identity")
    @Operation(summary = "获取身份", description = "根据房间号查询redis里的主人账号从而获取自己是不是主人")
    @Parameters({@Parameter(name = "roomId", description = "房间号"),
            @Parameter(name = "account", description = "账号"),
    })
    public Result getMyIdentity(@RequestParam("roomId") String roomId, @RequestParam("account") String account) {
        return listenMusicTogetherService.getMyIdentity(roomId, account);
    }

    @GetMapping("/ownerInfo")
    @Operation(summary = "获取房间主人的信息", description = "根据房间号查询redis里的主人账号 再调feign从而获取房间主人的信息")
    @Parameters({@Parameter(name = "roomId", description = "房间号"),
    })
    public Result getOwnerInfo(@RequestParam("roomId") String roomId) {
        return listenMusicTogetherService.getOwnerInfo(roomId);
    }
    @GetMapping("/visitorInfo")
    @Operation(summary = "获取房间客人的信息", description = "根据房间号查询redis里的客人账号 再调feign从而获取房间客人的信息")
    @Parameters({@Parameter(name = "roomId", description = "房间号"),
    })
    public Result getVisitorInfo(@RequestParam("roomId") String roomId) {
        return listenMusicTogetherService.getVisitorInfo(roomId);
    }

    @GetMapping("/sheet_musicList")
    @Operation(summary = "获取房间主人的音乐列表", description = "根据房间号查询redis里的主人账号从而获取房间主人的音乐列表")
    @Parameters({@Parameter(name = "roomId", description = "房间号"),
            @Parameter(name = "sheetId", description = "歌单id，如果为null就代表使用默认列表（喜欢歌单）"),
    })
    public Result getOwnerMusicList(@RequestParam("roomId") String roomId, @RequestParam("sheetId") String sheetId) {
        return listenMusicTogetherService.getOwnerMusicList(roomId, sheetId);
    }
}